In [3]:
import os
import sys
os.environ["CUDA_VISIBLE_DEVICES"]="0"
sys.path.insert(1,'/home/jgozlan/GIT/scripts/')
sys.path.insert(1,'/home/jgozlan/GIT/models/')
sys.path.insert(1, '/home/jgozlan/GIT/models_analysis/')
#from tensorflow import keras
import keras
from keras import losses
from keras.callbacks import EarlyStopping
from keras.optimizers import adam
from keras.models import Model, Sequential
from keras import backend as K
from keras.metrics import MeanAbsolutePercentageError

import matplotlib.pyplot as plt
import numpy as np
import samplerate
from numpy import array, hstack, unique, where
import statsmodels.api as sm 

import tensorflow as tf
tf.test.is_gpu_available()

from imu_extractor import get_all_dataframes_for_forecasting, get_freq_magn, transform_dataset_into_freq_magn
from train_test_split import get_train_test_split
from baseline import find_frequency,repeat_period_forecast_acf, get_mean_line_forecast, get_flat_line_forecast
from metrics import compare_method, plot_few_prediction
from models import create_lstm_encoder_decoder
Using TensorFlow backend.
In [4]:
path_cluster0 = ['Run_Features','Walk_Features']

path_cluster1 = ['CarDriving_Features']

path_cluster2 = ['Karting_Features',
                 'MotorcycleHelmet_Features',
                 'Scooter_Features','SkateboardChesty_Features',
                 'SnowboardSeeker_Features','Hiking_Features']

path_all_classes = path_cluster0 + path_cluster1 + path_cluster2

recording_freq = 6400.0
downsampling_freq = 200.0

all_dfs_classes = []

for i,c in enumerate(path_all_classes):
    dfs = get_all_dataframes_for_forecasting("Database/" + c, recording_freq, downsampling_freq)
    all_dfs_classes.append(dfs)
    
all_df = [item for sublist in all_dfs_classes for item in sublist]
GH010029.MP4.eis_dump.bin_features.json
GH010011.MP4.eis_dump.bin_features.json
GH019983.MP4.eis_dump.bin_features.json
GH010019.MP4.eis_dump.bin_features.json
GH010030.MP4.eis_dump.bin_features.json
GH010028.MP4.eis_dump.bin_features.json
GH019982.MP4.eis_dump.bin_features.json
GH010012.MP4.eis_dump.bin_features.json
GH019973.MP4.eis_dump.bin_features.json
GH010018.MP4.eis_dump.bin_features.json
GH019969.MP4.eis_dump.bin_features.json
GH010034.MP4.eis_dump.bin_features.json
GH010014.MP4.eis_dump.bin_features.json
GH019975.MP4.eis_dump.bin_features.json
GH010004.MP4.eis_dump.bin_features.json
GH010005.MP4.eis_dump.bin_features.json
GH010019.MP4.eis_dump.bin_features.json
GH019968.MP4.eis_dump.bin_features.json
GH010035.MP4.eis_dump.bin_features.json
GH019976.MP4.eis_dump.bin_features.json
GH010013.MP4.eis_dump.bin_features.json
GH019980.MP4.eis_dump.bin_features.json
GH010012.MP4.eis_dump.bin_features.json
GX010006.MP4.eis_dump.bin_features.json
filtering median....
GX010002.MP4.eis_dump.bin_features.json
filtering median....
GH010013.MP4.eis_dump.bin_features.json
filtering median....
GX010003.MP4.eis_dump.bin_features.json
filtering median....
GH010012.MP4.eis_dump.bin_features.json
filtering median....
GH010008.MP4.eis_dump.bin_features.json
filtering median....
GX010005.MP4.eis_dump.bin_features.json
filtering median....
GH010010.MP4.eis_dump.bin_features.json
filtering median....
GX010004.MP4.eis_dump.bin_features.json
filtering median....
GH010011.MP4.eis_dump.bin_features.json
filtering median....
GH010009.MP4.eis_dump.bin_features.json
filtering median....
GX010018.MP4.eis_dump.bin_features.json
GX010020.MP4.eis_dump.bin_features.json
GX010008.MP4.eis_dump.bin_features.json
GX010010.MP4.eis_dump.bin_features.json
GX010014.MP4.eis_dump.bin_features.json
GX010015.MP4.eis_dump.bin_features.json
GX010019.MP4.eis_dump.bin_features.json
GX010009.MP4.eis_dump.bin_features.json
GX010013.MP4.eis_dump.bin_features.json
GX010017.MP4.eis_dump.bin_features.json
GX010016.MP4.eis_dump.bin_features.json
GH010069.MP4.eis_dump.bin_features.json
GH010079.MP4.eis_dump.bin_features.json
GH010075.MP4.eis_dump.bin_features.json
GH010071.MP4.eis_dump.bin_features.json
GH010074.MP4.eis_dump.bin_features.json
GH010070.MP4.eis_dump.bin_features.json
GH010068.MP4.eis_dump.bin_features.json
GH010078.MP4.eis_dump.bin_features.json
GH010076.MP4.eis_dump.bin_features.json
GH010072.MP4.eis_dump.bin_features.json
GH010077.MP4.eis_dump.bin_features.json
GH010073.MP4.eis_dump.bin_features.json
GH010067.MP4.eis_dump.bin_features.json
GX010113.MP4.eis_dump.bin_features.json
GH010017.MP4.eis_dump.bin_features.json
GX010116.MP4.eis_dump.bin_features.json
GX010112.MP4.eis_dump.bin_features.json
GH010016.MP4.eis_dump.bin_features.json
GX010108.MP4.eis_dump.bin_features.json
GX010114.MP4.eis_dump.bin_features.json
GX010111.MP4.eis_dump.bin_features.json
GH010015.MP4.eis_dump.bin_features.json
GX010109.MP4.eis_dump.bin_features.json
GH010049.MP4.eis_dump.bin_features.json
GH010055.MP4.eis_dump.bin_features.json
GH010051.MP4.eis_dump.bin_features.json
GH010054.MP4.eis_dump.bin_features.json
GH010050.MP4.eis_dump.bin_features.json
GH010048.MP4.eis_dump.bin_features.json
GH010056.MP4.eis_dump.bin_features.json
GH010052.MP4.eis_dump.bin_features.json
GH010046.MP4.eis_dump.bin_features.json
GH010047.MP4.eis_dump.bin_features.json
GX010115.MP4.eis_dump.bin_features.json
GX010111.MP4.eis_dump.bin_features.json
GX010119.MP4.eis_dump.bin_features.json
GX010118.MP4.eis_dump.bin_features.json
GX010114.MP4.eis_dump.bin_features.json
GX010116.MP4.eis_dump.bin_features.json
GX010112.MP4.eis_dump.bin_features.json
GX010117.MP4.eis_dump.bin_features.json
GX010113.MP4.eis_dump.bin_features.json
GX010005.MP4.eis_dump.bin_features.json
GX010001.MP4.eis_dump.bin_features.json
GX010009.MP4.eis_dump.bin_features.json
GX010008.MP4.eis_dump.bin_features.json
GX010006.MP4.eis_dump.bin_features.json
GX010002.MP4.eis_dump.bin_features.json
GX010003.MP4.eis_dump.bin_features.json
In [5]:
lag = 500
ahead = 100
delay = 5
test_size = 0.2
dim = 12
target_index = [0,1,2]
classification = False

X_train, y_train, X_test, y_test = get_train_test_split(all_df, test_size, lag, ahead, delay, target_index, classification = False)
In [6]:
num_freq = 12

transformed_X_train = transform_dataset_into_freq_magn(X_train[:,:,:6], downsampling_freq, num_freq)
transformed_X_test = transform_dataset_into_freq_magn(X_test[:,:,:6], downsampling_freq, num_freq)

freq_dim_in = transformed_X_train.shape[1]
In [9]:
transformed_X_test.shape
Out[9]:
(46325, 144)
In [7]:
def create_dropout_predict_function(model, dropout):
    """
    Create a keras function to predict with dropout
    model : keras model
    dropout : fraction dropout to apply to all layers
    
    Returns
    predict_with_dropout : keras function for predicting with dropout
    """
    
    # Load the config of the original model
    conf = model.get_config()
    # Add the specified dropout to all layers
    for layer in conf['layers']:
        # Dropout layers
        if layer["class_name"]=="Dropout":
            #print("1")
            layer["config"]["rate"] = dropout
        # Recurrent layers with dropout
        elif "dropout" in layer["config"].keys():
            #print("2")
            #print(layer)
            #print(layer["config"]["dropout"])
            layer["config"]["dropout"] = dropout

    # Create a new model with specified dropout
    if type(model)==Sequential:
        # Sequential
        model_dropout = Sequential.from_config(conf)
    else:
        # Functional
        model_dropout = Model.from_config(conf)
    model_dropout.set_weights(model.get_weights()) 
    
    # Create a function to predict with the dropout on
    predict_with_dropout = K.function(model_dropout.inputs+[K.learning_phase()], model_dropout.outputs)
    
    return predict_with_dropout


def get_stats_from_pred(pred_with_dropout, input_pred, num_iter, ci = 0.8):

    predictions = np.zeros((num_iter, ahead))
    
    for i in range(num_iter):
        
        pred = pred_with_dropout((input_pred ,1.0))
        predictions[i,:] = pred[0].reshape((1,50))
        
    means = predictions.mean(axis=0)
    sds = predictions.std(axis = 0)
    
    lows = np.quantile(predictions, 0.5-ci/2, axis=0)
    uppers = np.quantile(predictions, 0.5+ci/2, axis=0)
    
    return means, sds, lows, uppers


def get_mean_std_ci_from_pred(pred_with_dropout, input_pred, num_iter = 30, ci = 0.8):
    
    predictions = np.zeros((num_iter, ahead,3))

    for i in range(num_iter):
        pred = pred_with_dropout((input_pred ,1.0))
        predictions[i,:] = pred[0].reshape((1,ahead,3))

    means_x = predictions[:,:,0].reshape((-1,100)).mean(axis=0)
    means_y = predictions[:,:,1].reshape((-1,100)).mean(axis=0)
    means_z = predictions[:,:,2].reshape((-1,100)).mean(axis=0)

    sds_x = predictions[:,:,0].reshape((-1,100)).std(axis=0)
    sds_y = predictions[:,:,1].reshape((-1,100)).std(axis=0)
    sds_z = predictions[:,:,2].reshape((-1,100)).std(axis=0)


    lows_x = np.quantile(predictions[:,:,0], 0.5- ci /2, axis=0)
    uppers_x = np.quantile(predictions[:,:,0], 0.5+ ci /2, axis=0)

    lows_y = np.quantile(predictions[:,:,1], 0.5- ci /2, axis=0)
    uppers_y = np.quantile(predictions[:,:,1], 0.5+ ci /2, axis=0)

    lows_z = np.quantile(predictions[:,:,2], 0.5- ci /2, axis=0)
    uppers_z = np.quantile(predictions[:,:,2], 0.5+ ci /2, axis=0)
    
    means = [means_x, means_y, means_z]
    sds = [sds_x, sds_y, sds_z]
    lows = [lows_x, lows_y, lows_z]
    uppers = [uppers_x, uppers_y, uppers_z]

    return means, sds, lows, uppers



# MODELS IMPORTcluster4_classification_model_new

cluster_classification_model = keras.models.load_model('cluster4_classification_model_new')

#global_forecasting_model = keras.models.load_model('models_analysis/global_forecasting_multi_5step',custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)

cluster0_model = keras.models.load_model('cluster0_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster1_model = keras.models.load_model('cluster1_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster2_model = keras.models.load_model('cluster2_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster3_model = keras.models.load_model('cluster3_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)


dropout = 0.05
cluster0_predict_with_dropout = create_dropout_predict_function(cluster0_model, dropout)
cluster1_predict_with_dropout = create_dropout_predict_function(cluster1_model, dropout)
cluster2_predict_with_dropout = create_dropout_predict_function(cluster2_model, dropout)
cluster3_predict_with_dropout = create_dropout_predict_function(cluster3_model, dropout)


cluster_models = [cluster0_model, cluster1_model, cluster2_model, cluster3_model]
cluster_dropout_models = [cluster0_predict_with_dropout, cluster1_predict_with_dropout, cluster2_predict_with_dropout, cluster3_predict_with_dropout]
WARNING:tensorflow:From /home/jgozlan/.conda/envs/base_cvdev_jeremygoz/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /home/jgozlan/.conda/envs/base_cvdev_jeremygoz/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

In [ ]:
global_forecast = global_forecasting_model.predict(X_test)
In [8]:
def get_acf_for_signal(input_, lag):

    acf = sm.tsa.acf(input_, nlags = lag) 
    inflection = np.diff(np.sign(np.diff(acf)))
    peaks = (inflection < 0).nonzero()[0] + 1
    acf_peaks = np.argsort(-1*acf[peaks])
    acf_peaks_sorted = acf[peaks][acf_peaks]
    if len(acf_peaks_sorted) > 0:
        return acf_peaks_sorted[0]
    else:
        return 0

def get_acf_value(samples, lag, delay):
    
    reconstitued_signal_x = np.zeros((lag + (len(samples)-1)*delay))
    reconstitued_signal_y = np.zeros((lag + (len(samples)-1)*delay))
    reconstitued_signal_z = np.zeros((lag + (len(samples)-1)*delay))

    for i,sample in enumerate(samples):
        if i != len(samples)-1:
            reconstitued_signal_x[(i*delay): (i*delay) + delay] = sample[:,0][:5]
            reconstitued_signal_y[(i*delay): (i*delay) + delay] = sample[:,1][:5]
            reconstitued_signal_z[(i*delay): (i*delay) + delay] = sample[:,2][:5]
        else: 
            reconstitued_signal_x[(i*delay):] = sample[:,0]
            reconstitued_signal_y[(i*delay):] = sample[:,1]
            reconstitued_signal_z[(i*delay):] = sample[:,2]

    #print(reconstitued_signal_x.shape)

    x_peak = get_acf_for_signal(reconstitued_signal_x, lag)
    y_peak = get_acf_for_signal(reconstitued_signal_y, lag)
    z_peak = get_acf_for_signal(reconstitued_signal_z, lag)

    return np.mean([x_peak,y_peak, z_peak])
In [9]:
def  repeat_period_forecast_acf_new(input_, lag, ahead, i):
    
    acf = sm.tsa.acf(input_, nlags = lag) 
    inflection = np.diff(np.sign(np.diff(acf)))
    peaks = (inflection < 0).nonzero()[0] + 1
    acf_top_peak = np.argsort(-1*acf[peaks])
    if len(acf_top_peak) < 1:
        return np.ones(ahead) * input_[-1:]
    top_peak_index = peaks[acf_top_peak][0]
    
    last_period_value = input_[-top_peak_index:]
    if ahead <= top_peak_index:
        #print(last_period_value[:ahead])
        return last_period_value[:ahead]
    else:
        nb_period = int(ahead/top_peak_index)
        repeated_period = np.array(last_period_value, copy=True)
        for i in range(nb_period -1):
            repeated_period = np.concatenate((repeated_period, last_period_value), axis = None)
        extra = ahead % top_peak_index
        if extra:
            repeated_period = np.concatenate((repeated_period, last_period_value[:extra]), axis = None)
        #print(repeated_period)
        return repeated_period
In [10]:
predictions_cluster0 = cluster0_model.predict(X_test)
predictions_cluster1 = cluster1_model.predict(X_test)
predictions_cluster2 = cluster2_model.predict(X_test)
predictions_cluster3 = cluster3_model.predict(X_test)
In [11]:
predictions_clusters = [predictions_cluster0, predictions_cluster1, predictions_cluster2, predictions_cluster3]
In [62]:
batch_pipeline_size = 100
threshold_softmax  = 0.75
threshold_acf = 0.6

predictions_cluster_class_acf = np.zeros((X_test.shape[0], ahead, 3))
predictions_cluster_class_acf_index = np.ones((X_test.shape[0])) * -1
predictions_cluster_class = np.zeros((X_test.shape[0], ahead, 3))


for i in range(0, len(X_test), batch_pipeline_size):
    
    if (i % 500) == 0:
        print(i)

    batch_time = X_test[i: (i+ batch_pipeline_size)]
    batch_freq = transformed_X_test[i: (i+ batch_pipeline_size)]

    cluster_probs = cluster_classification_model.predict(batch_freq)
    cluster_probs_filtered = cluster_probs > threshold_softmax
    idx0 = np.where(cluster_probs_filtered, cluster_probs ,np.nanmin(cluster_probs)-threshold_softmax).argmax(1)
    idx = np.where(cluster_probs_filtered.any(1), idx0, -1)

    if all(x == idx[0] for x in idx) and idx[0] != -1:

        acf_value = get_acf_value(batch_time[:,:,:3], lag, delay)
        predictions_cluster_class[i:i+batch_pipeline_size] = predictions_clusters[idx[0]][i: i + batch_pipeline_size]
        if acf_value > threshold_acf:

            predictions_cluster_class_acf[i: i + batch_pipeline_size] = predictions_clusters[idx[0]][i: i + batch_pipeline_size]
            predictions_cluster_class_acf_index[i: i + batch_pipeline_size] = idx[0]
0
500
1000
1500
2000
2500
3000
3500
4000
4500
5000
5500
6000
6500
7000
7500
8000
8500
9000
9500
10000
10500
11000
11500
12000
12500
13000
13500
14000
14500
15000
15500
16000
16500
17000
17500
18000
18500
19000
19500
20000
20500
21000
21500
22000
22500
23000
23500
24000
24500
25000
25500
26000
26500
27000
27500
28000
28500
29000
29500
30000
30500
31000
31500
32000
32500
33000
33500
34000
34500
35000
35500
36000
36500
37000
37500
38000
38500
39000
39500
40000
40500
41000
41500
42000
42500
43000
43500
44000
44500
45000
45500
46000
In [64]:
idx_threshold = np.where(predictions_cluster_class_acf_index != -1)[0]

ratio = len(idx_threshold)/X_test.shape[0]
print(ratio)
0.15542363734484618
In [54]:
import numpy as np

forecast = predictions_cluster_class
forecast_x = forecast[:,:,0].reshape((forecast.shape[0], forecast.shape[1]))
forecast_y = forecast[:,:,1].reshape((forecast.shape[0], forecast.shape[1]))
forecast_z = forecast[:,:,2].reshape((forecast.shape[0], forecast.shape[1]))

forecast_acf = predictions_cluster_class_acf
forecast_x_acf = forecast_acf[:,:,0].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))
forecast_y_acf = forecast_acf[:,:,1].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))
forecast_z_acf = forecast_acf[:,:,2].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))


forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_z_period_acf = np.array(forecast_z_period_acf)



true_x = y_test[:,:,0].reshape((y_test.shape[0], y_test.shape[1]))
true_y = y_test[:,:,1].reshape((y_test.shape[0], y_test.shape[1]))
true_z = y_test[:,:,2].reshape((y_test.shape[0], y_test.shape[1]))
In [56]:
import random
from evaluation2 import plot_forecasts2, plot_metrics
import matplotlib.pyplot as plt

def plot_forecasts_test(X_true, y_true, forecasts, forecasts_color_dict, target_index, k):
    
    random_indexes = random.sample(range(X_true.shape[0]), k)
    
    for i in random_indexes:
        
        plt.figure()
        
        X_input = X_true[i][:,target_index].reshape((lag,-1))[-100:]
        t = [len(X_input) + j for j in range(len(y_true[i]))]

        plt.plot(range(len(X_input)), X_input, label = 'x_gyro input', color = "green")
        plt.plot(t, y_true[i], label = 'x_gyro_true', color = "green")
        
        for name_forecast, forecast in forecasts.items():
            plt.plot(t, forecast[i], label = name_forecast, color = dict_forecasts_color[name_forecast])

        plt.legend(loc="upper left")
        
    plt.show()
In [57]:
dict_forecasts_x = {"forecast": forecast_x, "forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast": forecast_y,"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast": forecast_z,"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
In [58]:
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test, true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [59]:
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))

forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)

true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
In [60]:
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
In [61]:
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [65]:
idx_threshold = np.where(predictions_cluster_class_acf_index == 0)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))

forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)

true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))

dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}

plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [66]:
idx_threshold = np.where(predictions_cluster_class_acf_index == 1)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))

forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)

true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))

dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}

plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [67]:
idx_threshold = np.where(predictions_cluster_class_acf_index == 2)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))

forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)

true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))

dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}

plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [68]:
idx_threshold = np.where(predictions_cluster_class_acf_index == 3)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))

forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)

forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)

forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)

true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))

dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}

plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
In [11]:
import numpy as np

cluster_forecasting_pred = np.zeros((X_test.shape[0], ahead, 1 ))

index_cluster0 = np.where(predicted_cluster_classes == 0)[0]
index_cluster1 = np.where(predicted_cluster_classes == 1)[0]
index_cluster2 = np.where(predicted_cluster_classes == 2)[0]

cluster_forecasting_pred[index_cluster0] = forecasting_cluster0_model.predict(X_test[index_cluster0])
cluster0_forecasting_pred[index_cluster1] =

#for i,pred in np.ndenumerate(predicted_cluster_classes):
    #cluster_forecasting_pred[i] = global_forecasting_model.predict(X_test[i].reshape((1,lag, dim)))
    
In [13]:
 
Out[13]:
array([ 6660,  6661,  6662, ..., 15499, 15500, 15501])
In [ ]:
global_forecasting_pred = global_forecasting_pred.reshape((global_forecasting_pred.shape[0],global_forecasting_pred.shape[1]))
cluster_forecasting_pred = cluster_forecasting_pred.reshape((cluster_forecasting_pred.shape[0],cluster_forecasting_pred.shape[1]))
cluster0_forecasting_pred = cluster0_forecasting_pred.reshape((cluster0_forecasting_pred.shape[0],cluster0_forecasting_pred.shape[1]))
cluster1_forecasting_pred = cluster1_forecasting_pred.reshape((cluster1_forecasting_pred.shape[0],cluster1_forecasting_pred.shape[1]))
cluster2_forecasting_pred = cluster2_forecasting_pred.reshape((cluster2_forecasting_pred.shape[0],cluster2_forecasting_pred.shape[1]))

forecast_flat = [get_flat_line_forecast(X_test[i][:, 0][-1], ahead) for i in range(X_test.shape[0])]
forecast_flat = np.array(forecast_flat)

forecast_period_acf = [repeat_period_forecast_acf(X_test[i][:, 0], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_period_acf = np.array(forecast_period_acf)

true = y_test.reshape((-1,ahead))
In [ ]:
dict_forecasts = {"forecast_global": global_forecasting_pred,
                   "forecast_cluster": cluster_forecasting_pred,
                   "forecast_cluster0": cluster0_forecasting_pred,
                   "forecast_cluster1": cluster1_forecasting_pred,
                   "forecast_cluster2": cluster2_forecasting_pred,
                    "flat": forecast_flat,
                   "period acf": forecast_period_acf}

dict_forecasts_color = {"forecast_global": "black",
                  "forecast_cluster": "blue",
                  "forecast_cluster0": "yellow",
                  "forecast_cluster1": "purple",
                  "forecast_cluster2": "orange",
                  "flat": "red",
                  "period acf": "brown"}
In [ ]:
compare_method(true, dict_forecasts)
In [ ]:
plot_few_prediction(X_test, true, dict_forecasts, 20)